﻿#region itdc

/************************************************************************
*Copyright(C) 2015 by qsgy
 *All rights reserved. 
 *FileName:     $safeitemname$
 *Author:       oyh
 *Version:      
 *UnityVersion：
 *Date:   $time$      
 *Description:    
 *History: 
************************************************************************/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeshDialog : MonoBehaviour,Receiver<string[]> {
    public TextMesh speak;
    [Tooltip("追随的目标.父级物体.可空")]
    public Transform follow;
    [Tooltip("局部坐标")]
    public Vector3 localPos=new Vector3();
    [Tooltip("要说的话。段落"), Multiline(3)]
    public string[] words;
    [Tooltip("字段显示时长")]
    public float saveTime = 1f;
    [Tooltip("循环次数，负数为无限次")]
    public int loop = 1;
    private int currentLoop;


    private void Awake()
    {
        currentLoop = loop;//初始化循环次数
    }
    //show
    private IEnumerator StartShow()
    {
        if (follow != null)
        {
            transform.parent = follow;
            transform.localPosition = localPos;
        }
#if DEBUG
        else { Debug.Log("没有为meshdialog指定父级"); }
#endif
        yield return 1;
        while (currentLoop != 0)
        {

            for (int i = 0; i < words.Length; i++)
            {
                speak.text = words[i];
                yield return new WaitForSeconds( saveTime);
                //看来被小土著给关起来了
                //呵呵，这种科技怎么能关住我？
            }
            currentLoop--;
        }
        gameObject.SetActive(false);
    }


    //退出显示
    private void OnDisable()
    {
        StopCoroutine("Start");

    }
    //启动显示
    private void OnEnable()
    {
        currentLoop = loop;//重置循环次数
        StartCoroutine(StartShow());
    }

    //将接受的信息存为显示的文字
    public void Reveive(TMessage<string[]> t)
    {
        words = t.Message;
    }
}
